
//
//  HomeContainerView.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "HomeContainerView.h"
#import "PageCollectionViewLayout.h"
#define ZOOM_MAX_SCALE 5.0
#define ZOOM_MIN_SCALE 1.0

@interface HomeContainerView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>



/** 配置文件 **/
@property(nonatomic,strong) NSArray *settingPlist;


/** 判断是否横竖屏 **/
@property(nonatomic,assign) BOOL isRight;

/** 是否第一次横竖屏 **/
@property(nonatomic,assign) BOOL WW;

/** nsarry **/
@property(nonatomic,strong) NSArray *titleArray;

@end

@implementation HomeContainerView

- (instancetype)init{
    
    if ([super init]) {
        
        self.titleArray = @[@"高清",@"标清",@"流畅"];
        self.isRight = NO;
        [self masLayoutSubview];
        
    }
    
    return self;
    
}

- (void)monitor:(Monitor *)monitor gesturePinched:(CGFloat)scale
{

    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.settingPlist.count;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [self.settingPlist[section]count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    DoorSystemHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DoorSystemHomeCollectionViewCell" forIndexPath:indexPath];
    [cell setupViewWithConfig:self.settingPlist[indexPath.section][indexPath.row]];
    if (self.isRight==NO)
    cell.iconNicknameLabel.textColor = [UIColor grayColor];
    else
    cell.iconNicknameLabel.textColor = [UIColor whiteColor];
    cell.iconButton.selected = [self.ALLstausArray[indexPath.row] boolValue];
    
    return cell;
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.delegate) {
        [self.delegate homeCollectionView:collectionView didSelectItemAtIndexPath:indexPath];
    }
    
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1.f;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isRight == NO) {
    return CGSizeMake((collectionView.frame.size.width/3-1.f), (collectionView.frame.size.height/2-6) );
    }else{
    return CGSizeMake((collectionView.frame.size.width/2-1.f), (collectionView.frame.size.height/3-6) );
    }
    
    
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// cell点击变色
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate) {
        [self.delegate homeCollectionView:collectionView didHighlightItemAtIndexPath:indexPath];
    }

}

- (void)collectionView:(UICollectionView *)collectionView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate) {
        [self.delegate homeCollectionView:collectionView didUnhighlightItemAtIndexPath:indexPath];
    }

}






- (UIActivityIndicatorView *)testActivityIndicator{
    
    if (!_testActivityIndicator) {
        
        __weak UIView *superView = self;
        _testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [superView addSubview:_testActivityIndicator];
        
    }
    return _testActivityIndicator;
}

- (Monitor *)monitorPortrait{
    
    if (!_monitorPortrait) {
        __weak UIView *superView = self;

        _monitorPortrait = [[Monitor alloc]init];
        [_monitorPortrait setMinimumGestureLength:100 MaximumVariance:50];
        [_monitorPortrait setUserInteractionEnabled:YES];
        _monitorPortrait.contentMode = UIViewContentModeScaleToFill;
        _monitorPortrait.backgroundColor = [UIColor blackColor];
        _monitorPortrait.delegate = self;
        [superView addSubview:_monitorPortrait];
    }
    return _monitorPortrait;
}




- (UIButton *)landscapeBtn{
    
    if (!_landscapeBtn) {
        
        _landscapeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _landscapeBtn.frame = CGRectMake(0, 0, 50, 50);
        [_landscapeBtn setBackgroundImage:[UIImage imageNamed:@"icon_quanping"] forState:UIControlStateNormal];
        [_landscapeBtn addTarget:self action:@selector(landscapeEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_landscapeBtn];


    }
    return _landscapeBtn;
}

- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        __weak UIView *superView = self;
        PageCollectionViewLayout *flowLayout = [[PageCollectionViewLayout alloc]init];
        flowLayout.itemCountPerRow = 3;
        flowLayout.rowCount = 2;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [superView addSubview:_collectionView];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[DoorSystemHomeCollectionViewCell class]
            forCellWithReuseIdentifier:@"DoorSystemHomeCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    
    return _collectionView;
    
}

- (NSArray *)ALLstausArray{
    
    if (!_ALLstausArray) {
        
        
        _ALLstausArray = @[@(NO),@(NO),@(NO),@(NO),@(NO),@(NO)];
    }
    
    return _ALLstausArray;
}

- (UIView *)HUDView{
    if (!_HUDView) {
        
        _HUDView = [[UIView alloc] init];
        _HUDView.backgroundColor = [[UIColor colorWithHexString:@"bf231815"]colorWithAlphaComponent:0.2];

        [self addSubview:_HUDView];
        
    }
    return _HUDView;
}

- (NSArray *)settingPlist{
    
    if (!_settingPlist) {
        
        _settingPlist = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"DoorSystemHome" ofType:@"plist"]];
        
    }
    
    return _settingPlist;
    
}
- (void)landScapeRightSubview{
    self.isRight = YES;
    if (_WW == YES) {
        return;
    }
    __weak UIView *superView = self;
    for (UIView *subview in [self subviews]) {
        if (subview.tag == 66) {
            [subview removeFromSuperview];
        }
    }
    self.collectionView.hidden = NO;
    self.collectionView.alpha = 1;
    self.tableView.alpha = 0;
    [self addSubview:self.HUDView];
    [self addSubview:self.tableView];
    [self bringSubviewToFront:self.collectionView];

    [self bringSubviewToFront:self.landscapeBtn];
    [self.monitorPortrait mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(superView);
        
    }];
    [self.HUDView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(80);
        make.width.mas_equalTo(pxScaleWidth(200));
       make.right.mas_equalTo(superView.mas_right);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(superView.mas_right);
        make.top.mas_equalTo(superView.mas_top);
        make.bottom.mas_equalTo(self.HUDView.mas_top);
        make.width.mas_equalTo(pxScaleWidth(200));
        
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.collectionView);
        
    }];
    self.collectionView.backgroundColor = [[UIColor colorWithHexString:@"bf231815"]colorWithAlphaComponent:0.2];
    [self.collectionView reloadData];

    
    [self.HUDView addSubview:self.HUDBtn];
    [self.HUDBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.HUDView.mas_left).mas_offset(18);
        make.bottom.mas_equalTo(self.HUDView.mas_bottom).mas_offset(-5);
        make.width.and.height.mas_equalTo(40);

        
    }];
    _WW = YES;
    self.HUDView.hidden = NO;

}
- (UIButton *)HUDBtn{
    
    if (!_HUDBtn) {
        
        _HUDBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _HUDBtn.tintColor = [UIColor whiteColor];
        [_HUDBtn setTitle:@"高清" forState:UIControlStateNormal];
        
        [_HUDBtn addTarget:self action:@selector(changeHUD) forControlEvents:UIControlEventTouchUpInside];
    }
    return _HUDBtn;
}

- (void)changeHUD{
    
    self.collectionView.alpha = 0;
    self.tableView.alpha = 1;

    [self bringSubviewToFront:self.tableView];
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self;
    _WW = NO;
    __weak typeof(self) weakself = self;
    self.HUDView.hidden = YES;
    self.tableView.hidden = NO;
    self.isRight = NO;
    self.collectionView.hidden = NO;
    self.landscapeBtn.hidden = NO;
    self.collectionView.alpha = 1;
    self.tableView.alpha = 0;
    [self.monitorPortrait mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(superView.mas_left);
        make.top.mas_equalTo(superView.mas_top);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(superView).multipliedBy(0.33);
    }];
    [self.landscapeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakself.monitorPortrait.mas_right).mas_offset(-15);
        make.bottom.mas_equalTo(weakself.monitorPortrait.mas_bottom).mas_offset(-15);
        make.width.and.height.mas_equalTo(25);
        
    }];
    [self bringSubviewToFront:self.landscapeBtn];

       [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakself.monitorPortrait.mas_bottom);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(superView).multipliedBy(0.4);
            
    }];
    
    [self.testActivityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(weakself.monitorPortrait.mas_centerX);
        make.centerY.mas_equalTo(weakself.monitorPortrait.mas_centerY);
        
    }];
   
    UIView *line1 = [[UIView alloc]init];
    UIView *line2 = [[UIView alloc]init];
    UIView *line3 = [[UIView alloc]init];
    UIView *line4 = [[UIView alloc]init];
    [self addSubview:line1];
    [self addSubview:line2];
    [self addSubview:line3];
    [self addSubview:line4];
    line1.tag = 66;
    line2.tag = 66;
    line3.tag = 66;
    line4.tag = 66;
    line1.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2f];
    line2.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2f];
    line3.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2f];
    line4.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2f];
    [line1 mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(superView.mas_left).mas_offset(SCREEN_WIDTH/3);
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(weakself.collectionView.mas_top);
        make.bottom.mas_equalTo(weakself.collectionView.mas_bottom);
        
    }];
    [line2 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(superView.mas_right).mas_offset(-SCREEN_WIDTH/3);
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(weakself.collectionView.mas_top);
        make.bottom.mas_equalTo(weakself.collectionView.mas_bottom);
        
    }];
    [line3 mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(weakself.collectionView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(1);
        
    }];
    [line4 mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(weakself.collectionView.mas_bottom);
        
    }];
    
    
    [self.logoImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-80);
        
    }];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView reloadData];

}
- (UIImageView *)logoImageView{
    
    if (!_logoImageView) {
        
        _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addlogo"]];
        [self addSubview:_logoImageView];
        
    }
    return _logoImageView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        //        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [[UIColor colorWithHexString:@"bf231815"]colorWithAlphaComponent:0.2];
    }
    
    return _tableView;
    
}

#pragma mark - --------------------------点击事件--------------------------
- (void)landscapeEvent:(UIButton *)sender{

    NSLog(@"点击了横屏");
    if (self.LandspaceEvent) {
        
        self.LandspaceEvent(sender);
        
    }

}



#pragma mark - --------------------------tableView--------------------------

#pragma mark - tableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.text = @"清晰度";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14.f];
    return cell;


}
#pragma mark - tableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
     
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
    }
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate) {
        [self.delegate homeTableView:tableView didSelectRowAtIndexPath:indexPath];
    }
    
}


@end
